﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using System.Data;
using Comm;
using BusinessFacade;
using Entities;

namespace MedicareNoteManagerWeb.Web
{
    public partial class StaffManageAdd : BasePage
    {
        Busi_pm_StaffManage model1 = new Busi_pm_StaffManage();
        public static string ID = "";//界面传值
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                model1 = (Busi_pm_StaffManage)Session["StaffManageModel"];
                BindDrop();
                try
                {
                    ID = "";
                    ID = Request.QueryString["ID"].ToString();
                }
                catch (Exception ex)
                {
                    lbl_Message.ToolTip = ex.Message;
                    lbl_Message.ToolTip = "";
                }
                if (ID != "")
                {
                    FillValue();
                }
            }
        }

        /// <summary>
        /// 填充值到界面
        /// </summary>
        public void FillValue()
        {
            Button4.Text = "修改";
            Busi_pm_StaffManage u = new Busi_pm_StaffManage();
            pm_StaffManage_Model model = u.GetListModel(Convert.ToInt32(ID));
            txtsafeName.Text = model.safeName;
            drpsafeState.SelectedValue = model.safeState;
            txtAge.Text = (model.Age).ToString();
            drpSex.SelectedValue = model.Sex;
            txtPayMentTerm.Text = model.paymentterm.ToString();
            txtArrearAgeTerm.Text = model.arrearageterm.ToString();
            txtPersonAccountNum.Text = model.personAccountNum;
            txtPayMentBase.Text = model.paymentbase.ToString();
            drpAremID.SelectedValue = model.aremID.ToString();
            txtTelPhone.Text = model.TelPhone;
            txtsafeCode.Text = model.safeCode;
            txtIDNumber.Text = model.IDNumber;
            txtConsume.Text = model.Consume.ToString();
        }

        /// <summary>
        /// 绑定区域
        /// </summary>
        public void BindDrop()
        {
            FillDropDownList("GetDrop", "AreaID", "AreaName", "请选择区域", this.drpAremID);
        }

        /// <summary>
        /// 判断文本值
        /// </summary>
        /// <returns></returns>
        public string CheckData()
        {
            string strErr = "";
            if (string.IsNullOrEmpty(txtsafeName.Text.Trim()))
            {
                strErr += "参保名称不为空";
                return strErr;
            }

            if (drpsafeState.SelectedValue == "0")
            {
                strErr += "请选择状态";
                return strErr;
            }

            if (string.IsNullOrEmpty(txtAge.Text.Trim()))
            {
                strErr += "年龄不为空";
                return strErr;
            }
            if (Convert.ToInt32(txtAge.Text) < 0)
            {
                strErr += "年龄必须为正数";
                return strErr;
            }

            if (drpSex.SelectedValue == "0")
            {
                strErr += "请选择性别";
                return strErr;
            }

            if (string.IsNullOrEmpty(txtPayMentTerm.Text.Trim()))
            {
                strErr += "缴费年限不为空";
                return strErr;
            }

            if (Convert.ToInt32(txtPayMentTerm.Text) < 0)
            {
                strErr += "缴费年限必须为正数";
                return strErr;
            }

            if (string.IsNullOrEmpty(txtArrearAgeTerm.Text.Trim()))
            {
                strErr += "欠费年限不为空";
                return strErr;
            }

            if (Convert.ToInt32(txtArrearAgeTerm.Text) < 0)
            {
                strErr += "欠费年限必须为正数";
                return strErr;
            }


            if (string.IsNullOrEmpty(txtPersonAccountNum.Text.Trim()))
            {
                strErr += "个人账号不为空";
                return strErr;
            }

            if (string.IsNullOrEmpty(txtPayMentBase.Text.Trim()))
            {
                strErr += "缴费基数不能为空";
                return strErr;
            }

            if (Convert.ToInt32(txtPayMentBase.Text) < 0)
            {
                strErr += "缴费基数必须为正数";
                return strErr;
            }

            if (string.IsNullOrEmpty(txtIDNumber.Text.Trim()))
            {
                strErr += "身份证号不为空";
                return strErr;
            }

            if (txtIDNumber.Text.Length < 17)
            {
                strErr += "身份证号不准确";
                return strErr;
            }

            if (drpAremID.SelectedValue == "0")
            {
                strErr += "请选择区域";
                return strErr;
            }

            if (string.IsNullOrEmpty(txtTelPhone.Text.Trim()))
            {
                strErr += "手机号码不为空";
                return strErr;
            }

            if (txtTelPhone.Text.Length < 10)
            {
                strErr += "电话号码不准确";
                return strErr;
            }

            if (string.IsNullOrEmpty(txtsafeCode.Text.Trim()))
            {
                strErr += "参保卡号不为空";
                return strErr;
            }
            return strErr;
        }

        /// <summary>
        /// 清空值
        /// </summary>
        protected void ClearValue()
        {
            txtsafeName.Text = "";
            drpsafeState.SelectedIndex = 0;
            txtAge.Text = "";
            drpSex.SelectedIndex = 0;
            txtPersonAccountNum.Text = "";
            txtPayMentTerm.Text = "";
            txtArrearAgeTerm.Text = "";
            txtPayMentBase.Text = "";
            drpAremID.SelectedIndex = 0;
            txtTelPhone.Text = "";
            txtsafeCode.Text = "";
            txtIDNumber.Text = "";
        }

        /// <summary>
        /// 添加和修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button4_Click(object sender, EventArgs e)
        {
            model1 = (Busi_pm_StaffManage)Session["StaffManageModel"];
            string strErr = CheckData();
            try
            {
                if (string.IsNullOrEmpty(strErr))
                {
                    if (ID == "")
                    {
                        sys_user_Model user = new sys_user_Model();
                        pm_StaffManage_Model model = new pm_StaffManage_Model();
                        model.safeName = txtsafeName.Text.ToString();
                        model.safeState = drpsafeState.SelectedValue.ToString();
                        model.Age = Convert.ToInt32(txtAge.Text.ToString());
                        model.Sex = drpSex.SelectedValue.ToString();
                        model.paymentterm = Convert.ToInt32(txtPayMentTerm.Text.ToString());
                        model.arrearageterm = Convert.ToInt32(txtArrearAgeTerm.Text.ToString());
                        model.personAccountNum = txtPersonAccountNum.Text.ToString();
                        model.paymentbase = Convert.ToDecimal(txtPayMentBase.Text.ToString());
                        model.Consume = Convert.ToDecimal(txtConsume.Text.ToString());
                        model.aremID = int.Parse(drpAremID.SelectedValue);
                        model.TelPhone = txtTelPhone.Text.ToString();
                        model.safeCode = txtsafeCode.Text.ToString();
                        model.IDNumber = txtIDNumber.Text.ToString();
                        model.createDate = DateTime.Now;
                        model.createManID = Convert.ToInt32(login.UserID);//user.UserID;
                        Busi_pm_StaffManage users = new Busi_pm_StaffManage();
                        DataTable ds = users.Get_IDNumber(model.IDNumber, model.safeCode).Tables[0];
                        if (!(ds == null || ds.Rows.Count == 0))
                        {
                            MessageBox.ShowDialog(Page, "数据已存在！", "error");
                            return;
                        }
                        if (new Busi_pm_StaffManage().Add(model))
                        {
                            MessageBox.ShowDialog(this.Page, "添加成功！", "success");
                            lbl_Message.Text = "添加成功！";
                            ClearValue();
                            return;
                        }
                        else
                        {
                            MessageBox.ShowDialog(this.Page, "添加失败！", "error");
                        }
                    }
                    else
                    {
                        try
                        {
                            sys_user_Model user = new sys_user_Model();
                            pm_StaffManage_Model model = new pm_StaffManage_Model();
                            model.safeName = txtsafeName.Text.ToString();
                            model.safeState = drpsafeState.SelectedValue.ToString();
                            model.Age = Convert.ToInt32(txtAge.Text.ToString());
                            model.Sex = drpSex.SelectedValue.ToString();
                            model.paymentterm = Convert.ToInt32(txtPayMentTerm.Text.ToString());
                            model.arrearageterm = Convert.ToInt32(txtArrearAgeTerm.Text.ToString());
                            model.personAccountNum = txtPersonAccountNum.Text.ToString();
                            model.paymentbase = Convert.ToInt32(txtPayMentBase.Text.ToString());
                            model.Consume = Convert.ToInt32(0);
                            model.aremID = Convert.ToInt32(drpAremID.SelectedValue);
                            model.TelPhone = txtTelPhone.Text.ToString();
                            model.safeCode = txtsafeCode.Text.ToString();
                            model.IDNumber = txtIDNumber.Text.ToString();
                            model.updateDate = DateTime.Now;
                            model.updateManID = user.UserID;
                            model.safeID = Convert.ToInt32(ID);
                            Busi_pm_StaffManage users = new Busi_pm_StaffManage();
                            DataTable ds = users.Get_IDNumber(model.IDNumber, model.safeCode).Tables[0];
                            if (!(ds == null || ds.Rows.Count == 0))
                            {
                                MessageBox.ShowDialog(Page, "数据已存在！", "error");
                                return;
                            }
                            else
                            {
                                new Busi_pm_StaffManage().Update(model);
                                MessageBox.ShowDialog(this.Page, "修改成功！", "success");
                                lbl_Message.Text = "修改成功";
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.ShowDialog(Page, "修改失败！" + ex.Message, "error");
                        }
                    }
                }
                else
                {
                    lbl_Message.Text = strErr;
                }
            }
            catch (System.Exception ex)
            {
                lbl_Message.Text = ex.Message;
            }
        }
    }
}